<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

// function otx_config_init($db = null)
// {
// 	global $_XCONFIG;
// 	$_XCONFIG = array();

// 	$rows = otx_db_fetch_all("SELECT * FROM #system_variable#", null, $db);

// 	foreach ($rows as $row) {
// 		if ($row['serialized']) {
// 			$row['value'] = unserialize($row['value']);
// 		}

// 		$_XCONFIG[$row['name']] = $row['value'];
// 	}
// }

function otx_config_set($key, $value)
{
	$GLOBALS['_XCONFIG'][$key] = $value;
}

function otx_config_get($key, $default = null)
{
	if (isset($GLOBALS['_XCONFIG'][$key])) {
		return $GLOBALS['_XCONFIG'][$key];
	} else {
		return $default;
	}
}

function otx_config_write($key, $value)
{
	$vars['name'] = $key;
	$vars['value'] = is_array($value) ? serialize($value) : $value;
	$vars['serialized'] = is_array($value) ? 1 : 0;
	otx_db_replace('#system_config#', $vars);
}

function otx_config_string($key, $default = '')
{
	return strval(otx_config_get($key, $default));
}

function otx_config_int($key, $default = 0)
{
	return intval(otx_config_get($key, $default));
}

function otx_config_bool($key, $default = false)
{
	return otx_config_int($key) ? true : $default;
}

function otx_config_array($key, $default = array())
{
	return (array) otx_config_get($key, $default);
}
